package com.ce.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ce.server.common.RespPageBean;
import com.ce.server.mapper.AdminMapper;
import com.ce.server.mapper.WorkingMapper;
import com.ce.server.pojo.Position;
import com.ce.server.pojo.Role;
import com.ce.server.pojo.Working;
import com.ce.server.service.IWorkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-26
 */
@Service
public class WorkingServiceImpl extends ServiceImpl<WorkingMapper, Working> implements IWorkingService {

  @Autowired
  private WorkingMapper workingMapper;
  @Autowired
  private AdminMapper adminMapper;

  @Override
  public RespPageBean getAllRecords(Integer currentPage, Integer size, Date date, String last_next) {
    Calendar instance = Calendar.getInstance();
    Date newDate = null;
    IPage<Working> workingIPage = null;
    if (date != null) {
      instance.setTime(date);
      if (last_next.equals("last")) {
        instance.add(Calendar.DATE, -7);
        newDate = instance.getTime();
      } else if (last_next.equals("next")) {
        instance.add(Calendar.DATE, +7);
        newDate = instance.getTime();
      } else {
        newDate = date;
      }
/*adminMapper.getRolesWithId(no)*/
    } else {
      List<Map<String, Object>> maps = workingMapper.selectMaps(new QueryWrapper<Working>().select("max(sortDays)"));
      newDate = (Date) maps.get(0).get("max(sortDays)");
    }
    Page<Working> workingPage = new Page<>(currentPage, size);
    workingIPage = workingMapper.getAllRecords(workingPage, newDate);
    if (workingIPage.getTotal() != 0) {
      List<Working> workingList = workingIPage.getRecords().stream().map(work ->
      {
        List<Role> roles = adminMapper.getRolesWithId(work.getWno());
        List<String> stringRole = roles.stream().map(Role::getNameZh).collect(Collectors.toList());
        work.setAdminVo(work.getAdminVo().setPosition(new Position().setName(stringRole.toString())));
        return work;
      }).collect(Collectors.toList());
      return new RespPageBean(workingIPage.getTotal(), workingList);
    } else {
      workingIPage = getCurrentPage(currentPage, size);
      return new RespPageBean(workingIPage.getTotal(), workingIPage.getRecords());
    }
  }

  public IPage<Working> getCurrentPage(Integer currentPage, Integer size) {
    Date newDate = null;
    List<Map<String, Object>> maps = workingMapper.selectMaps(new QueryWrapper<Working>().select("max(sortDays)"));
    newDate = (Date) maps.get(0).get("max(sortDays)");
    Page<Working> workingPage = new Page<>(currentPage, size);
    IPage<Working> workingIPage = workingMapper.getAllRecords(workingPage, newDate);
    return workingIPage;
  }
}
